<?php
App::import ( 'Vendor', 'php-excel-reader/excel_reader2' ); //import statement

class ConfigurationsController extends AppController {
	
	var $name = 'Configurations';
	var $components = array ('Auth' ); // Not necessary if declared in your app controller
	var $layout = 'admin';
	
	/**
	 *  The AuthComponent provides the needed functionality
	 *  for login, so you can leave this function blank.
	 */
	function beforeFilter() {
		
		$this->Auth->loginAction = array ('controller' => 'users', 'action' => 'login' );
		$this->Auth->loginRedirect = array ('controller' => 'contents', 'action' => 'index' );
		$this->Auth->logoutRedirect = array ('controller' => 'users', 'action' => 'login' );
		$this->Auth->loginError = 'The information you\'ve entered is incorrect.';
		$this->Auth->logoutError = 'You\'ve successfully logged out.';
		$username = $this->Auth->user ();
		$this->set ( 'username', $username ['User'] ['name'] );
		$this->Auth->allow ( 'login', 'logout' );
		//echo  $this->Auth->password('a') ;6dd25d865031f6a39cc950ba6fedfbe26a9908b9 
	

	}
	
	public function index() {
		$params ['order'] = 'id';
		if (isset ( $this->params ['named'] ['orderBy'] )) {
			$params ['order'] = $this->params ['named'] ['orderBy'];
			$this->set ( 'orderBy', $this->params ['named'] ['orderBy'] );
			if (isset ( $this->params ['named'] ['DESC'] )) {
				$params ['order'] .= ' DESC';
				$this->set ( 'DESC', $this->params ['named'] ['DESC'] );
			}
		}
		$configurations = $this->Configuration->find ( 'all', $params );
		$this->set ( 'configurations', $configurations );
	}
	
	public function delete($id = NULL) {
		if ($id) {
			if ($this->Configuration->delete ( $id )) {
				$this->Session->setFlash ( 'Configuration item deleted successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed! Please try again or contact to administrator.' );
				$this->redirect ( array ('action' => 'index' ) );
			}
		
		} else {
			$this->Session->setFlash ( 'Failed! No such configuration item.' );
			$this->redirect ( array ('action' => 'index' ) );
		
		}
	}
	
	public function add() {
		if (! empty ( $this->data )) {
			$this->Configuration->create ();
			if ($this->Configuration->save ( $this->data )) {
				$this->Session->setFlash ( 'Configuration item added successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed to add configuration item!' );
			}
		}
	}
	
	public function edit($id) {
		if (! empty ( $this->data )) {
			$this->Configuration->id = $id;
			if ($this->Configuration->save ( $this->data )) {
				$this->Session->setFlash ( 'Configuration item edited successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed to edit configuration item!' );
			}
		} else {
			$this->data = $this->Configuration->findById ( $id );
		}
	}
	
	
}

?>